package com.caineng.zhaobiao.zbapi.utils.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨域问题
 */
@Configuration
public class CorsConfig {

	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("https://caineng.com");
		corsConfiguration.addAllowedOrigin("https://www.caineng.com");
		corsConfiguration.addAllowedOrigin("https://liandyao.vicp.net");
		corsConfiguration.addAllowedOrigin("http://lifenyao.vicp.net");
		corsConfiguration.addAllowedOrigin("http://localhost:8900");
		corsConfiguration.addAllowedOrigin("http://localhost:3000");
		corsConfiguration.addAllowedOrigin("http://192.168.2.125:8080");
		corsConfiguration.addAllowedOrigin("http://192.168.2.123");

		corsConfiguration.addAllowedOrigin("https://caineng.hsk.top");
		corsConfiguration.addAllowedOrigin("https://nifan.net");
		corsConfiguration.addAllowedOrigin("https://caop.com");
		corsConfiguration.addAllowedOrigin("http://121.89.226.205:6555");

		//2022年3月4日写的,联调完成之后需要注释掉这条信息
		//corsConfiguration.addAllowedOrigin("*");

		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("OPTIONS");
		corsConfiguration.addAllowedMethod("HEAD");
		corsConfiguration.addAllowedMethod("GET");
		corsConfiguration.addAllowedMethod("PUT");
		corsConfiguration.addAllowedMethod("POST");
		corsConfiguration.addAllowedMethod("DELETE");

		corsConfiguration.setMaxAge(3600L);         // 预检请求的有效期，单位为秒。
		corsConfiguration.setAllowCredentials(true);// 是否支持安全证书(必需参数)
		return corsConfiguration;
	}

	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}
}